//
//  GruphotoTabBarController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/4/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "GruphotoTabBarController.h"
#import "GruphotoTabBar.h"
#import "LiveViewController.h"
#import "PostViewController.h"
#import "SettingsViewController.h"
#import "JoinViewController.h"
#import "FindPlaceViewController.h"
#import "GruphotoAppDelegate.h"
#import <FacebookSDK/FacebookSDK.h>

@interface GruphotoTabBarController (PrivateMethods)
- (void)hidesTabBar;
- (void)facebookDidLoginToFindNearbyPlace:(NSNotification *)notification;
@end

@implementation GruphotoTabBarController
@synthesize fakeTabBar;

#pragma mark - Lifecycle
+ (id)tabBarFromNib {
    GruphotoTabBarController *tabbarController = nil;
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"GruphotoTabBarController" owner:self options:nil];
    tabbarController = (GruphotoTabBarController *)[nibs objectAtIndex:0];
    return tabbarController;
}

- (id)init {
    if (self = [super init]) {
        LiveViewController *liveViewController = [[LiveViewController alloc] initWithNibName:@"LiveViewController" bundle:nil];
        UINavigationController *liveNavigator = [[UINavigationController alloc] initWithRootViewController:liveViewController];
        [liveViewController release];
        
        PostViewController *postViewController = [[PostViewController alloc] initWithNibName:@"PostViewController" bundle:nil];
        UINavigationController *postNavigator = [[UINavigationController alloc] initWithRootViewController:postViewController];
        [postViewController release];
        
        SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
        UINavigationController *settingsNavigator = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
        [settingsViewController release];
        
        NSArray *tabBarViewControllers = [NSArray arrayWithObjects:liveNavigator, postNavigator, settingsNavigator, nil];
        self.viewControllers = tabBarViewControllers;
        [liveNavigator release], [postNavigator release], [settingsNavigator release];
        
        GruphotoTabBar *tabBar = [GruphotoTabBar tabBarFromNib];
        tabBar.frame = self.tabBar.frame;
        [self.view addSubview:tabBar];
        self.fakeTabBar = tabBar;
        tabBar.selectedItem = [tabBar.items objectAtIndex:0];
        tabBar.delegate = self;
        float y = [Global screenHeight];
        self.tabBar.frame = CGRectMake(0, y, 320, 49);
        
        if (!FBSession.activeSession.isOpen) {
            if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
                [FBSession.activeSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                    
                }];
            }
        }
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(facebookDidLoginToFindNearbyPlace:) name:GPFacebookSessionDidOpenedNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.fakeTabBar = nil;
    [super dealloc];
}

#pragma mark - Tabbar visibility
- (void)hidesFakeTabBar:(BOOL)animated {
    float y = [Global screenHeight];
    [UIView animateWithDuration:0.2 animations:^{
        fakeTabBar.frame = CGRectMake(0, y, 320, 49);
    }];
}

- (void)showFakeTabBar:(BOOL)animated {
    float y = [Global screenHeight];
    fakeTabBar.frame = CGRectMake(0, y - 49, 320, 49);
}

#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (tabBar == fakeTabBar) {
        switch (item.tag) {
            case 0:
            case 1:
            case 2:
                self.selectedIndex = item.tag;
                break;
            case 3: {
                JoinViewController *joinViewController = [[JoinViewController alloc] initWithNibName:@"JoinViewController" bundle:nil];
                UINavigationController *joinNavigator = [[UINavigationController alloc] initWithRootViewController:joinViewController];
                [joinViewController release];
                [self presentModalViewController:joinNavigator animated:YES];
                [joinNavigator release];
                NSUInteger itemIndex = self.selectedIndex;
                if (itemIndex == 2) {
                    itemIndex += 2;
                }
                [fakeTabBar performSelector:@selector(setSelectedItem:) withObject:[fakeTabBar.items objectAtIndex:itemIndex] afterDelay:0.5];
                break;
            }
            case 4: {
                if (FBSession.activeSession.isOpen) {
                    FindPlaceViewController *findPlaceViewController = [[FindPlaceViewController alloc] initWithNibName:@"FindPlaceViewController" bundle:nil];
                    UINavigationController *createNavigator = [[UINavigationController alloc] initWithRootViewController:findPlaceViewController];
                    [findPlaceViewController release];
                    [self presentModalViewController:createNavigator animated:YES];
                    [createNavigator release];
                } else {
                    facebookLoginType = GPFacebookLoginTypeFindNearbyPlace;
                    [[GruphotoAppDelegate sharedDelegate] openSessionWithAllowLoginUI:YES];
                }
                NSUInteger itemIndex = self.selectedIndex;
                if (itemIndex == 2) {
                    itemIndex += 2;
                }
                [fakeTabBar performSelector:@selector(setSelectedItem:) withObject:[fakeTabBar.items objectAtIndex:itemIndex] afterDelay:0.5];              
                break;
            }
            default:
                break;
        }
    }
}

#pragma mark - Facebook did login
- (void)facebookDidLoginToFindNearbyPlace:(NSNotification *)notification {
    if (facebookLoginType == GPFacebookLoginTypeFindNearbyPlace) {
        FindPlaceViewController *findPlaceViewController = [[FindPlaceViewController alloc] initWithNibName:@"FindPlaceViewController" bundle:nil];
        UINavigationController *createNavigator = [[UINavigationController alloc] initWithRootViewController:findPlaceViewController];
        [findPlaceViewController release];
        [self presentModalViewController:createNavigator animated:YES];
        [createNavigator release];
        NSUInteger itemIndex = self.selectedIndex;
        if (itemIndex == 2) {
            itemIndex += 2;
        }
        [fakeTabBar performSelector:@selector(setSelectedItem:) withObject:[fakeTabBar.items objectAtIndex:itemIndex] afterDelay:0.5];        
    }
}

@end
